const { globalShortcut, ipcMain, app, BrowserWindow, Tray, Menu } = require('electron');
const fs = require("fs");
const path = require('path');
const debug = /--debug/.test(process.argv[2])
let monitorWindow = new (require("./monitor_main"))(debug);
let settingWindow = new (require("./setting_main"))(debug);
let aboutWindow = new (require('./about_main'))(debug);

///////////////////////////////////////////////////////
// app start
function createUserDataFloder() {
    var paths = path.join(app.getPath("userData"),"data");
    if(!fs.existsSync(paths)) {
        fs.mkdirSync(paths);
    }
}

app.on('ready', function(){
    createUserDataFloder();
    monitorWindow.setAboutWindow(aboutWindow);
    monitorWindow.setSettingWindow(settingWindow);
    var win = monitorWindow.createWindow();
    globalShortcut.register('CommandOrControl+Alt+J', () => {
        if(!win.isFocused()||!win.isVisible()) {
            win.show();
        }
        else {
            win.hide();
        }
        monitorWindow.stopTrayFlicker();
    });
});
app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') app.quit();
});
app.on('activate', function () {
});


const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
    app.quit()
}
else {
    app.on('second-instance', (event, commandLine, workingDirectory) => {
        // 当运行第二个实例时,将会聚焦到myWindow这个窗口
        if (monitorWindow) {
            monitorWindow.show();
            monitorWindow.focus()
        }
      })
}